package com.bdmc.web.tcp;

import com.alibaba.fastjson.JSONObject;
import com.bdmc.common.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Slf4j
public class TcpClient {
    public static String ip = "36.134.79.195";
    public static String videoLivePort = "http://36.134.79.195:8103";
    public static int port = 8102;

    // 批量向设备发送直播消息
    public static String sendVideoLive(String simCard, String channel) {
        String url = "%s/video/%s-%s";
        String result;
        List<HashMap<String, Object>> list = new LinkedList<>();
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("IMEI", simCard);
        hash.put("CMD", "9101");
        hash.put("PARAM1",ip);
        hash.put("PARAM2", port);
        hash.put("PARAM3", "0");
        hash.put("PARAM4", channel);// 逻辑通道号2或者3
        hash.put("PARAM5", "0");
        hash.put("PARAM6", "1");
        list.add(hash);
        String json = JSONObject.toJSONString(list);
        RedisUtils.publish("DownSendDeviceChannel_808",json);
        // _redis.publisher("DownSendDeviceChannel_808", json);
        //NettyChannelHandler.send(list);
        // 不足12位要补0
        String simCardStr = String.format("%012d", Long.valueOf(simCard));
        result = String.format(url, videoLivePort, simCardStr, channel);
        log.info("DownSendDeviceChannel_808:" + json);
        return result;
    }

    // 向设备发送停止直播消息
    public static void sendVideoLiveStop(String simCard, String channel) {
        List<HashMap<String, Object>> list = new LinkedList<>();
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("IMEI", simCard);
        hash.put("CMD", "9102");
        hash.put("PARAM1", channel);// 通道号
        hash.put("PARAM2", "0");
        hash.put("PARAM3", "0");
        hash.put("PARAM4", "0");
        list.add(hash);
        String json = JSONObject.toJSONString(list);
        RedisUtils.publish("DownSendDeviceChannel_808",json);
        log.info("DownSendDeviceChannel_808:" + json);
    }
}
